﻿#include "serial_func.h"
#include <QDebug>

serial_func::serial_func(QObject *parent) :
    QThread(parent)
{
    setting_config = new QSettings(INICONFIG, QSettings::IniFormat, 0);
    openFlag = false;
    connect(this,SIGNAL(sig_readfinish(int)),this,SLOT(readfinish_slot(int)));
    resendNum = 0;
}

//串口打开
int serial_func::open()
{
    timer = new QTimer(this);
    QSerialPortInfo serialPortInfo;
//    serialPortInfo.
    serial = new QSerialPort(setting_config->value("Port").toString());
    serial->open(QIODevice::ReadWrite);
    QSerialPort::BaudRate brt;
    switch(setting_config->value("BaudRate").toInt()){
        case 9600:
            brt = QSerialPort::Baud9600;
            break;
        case 38400:
            brt = QSerialPort::Baud38400;
            break;
        case 115200:
            brt = QSerialPort::Baud115200;
            break;
        default:
            brt = QSerialPort::Baud38400;
    }
    serial->setBaudRate(brt);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    if(!serial->isOpen()){
        return -1;
    }
    connect(timer, SIGNAL(timeout()), this, SLOT(timerOut_slot()));

    qDebug()<<"uart open"<<brt;
    openFlag = true;
    return 0;
}

//串口关闭
void serial_func::close()
{
    if(serial->isOpen()){
        qDebug()<<"uart close";
        timer->stop();
        serial->close();
    }
    openFlag = false;
}

//串口发送数据
void serial_func::send(QByteArray buf)
{
    if(!serial->isOpen()){
        qDebug()<<"send:uart not open";
        return;
    }
    sendString = buf;
    serial->write(buf);
    start();
    int overtime = setting_config->value("OverTime").toInt()*1000;
    if(overtime == 0)
        overtime = 500;
    timer->start(overtime);
//    SyncNum++;
}

//获取接收数据接口
QByteArray serial_func::uart_getString()
{
    return getString;
}

//定时器槽
void serial_func::timerOut_slot()
{
    if(resendNum < 3){
        qDebug()<<"sendString";
        serial->write(sendString);
    }else{
        timer->stop();
        emit sig_readfinish(-1);
        resendNum = 0;
    }
    resendNum++;
//    qDebug()<<"timerOut_resent :"<<resendNum;
}

//串口接收槽
void serial_func::readfinish_slot(int)
{
    timer->stop();//关闭超时计时器
    quit();//接收完关闭线程
//    qDebug()<<"get data over!"<<e;
}

void serial_func::run()
{
    QByteArray get;
    while(serial->isOpen())
    {
        QByteArray temp = serial->readAll();

        if(!temp.isEmpty()){
            get = get + temp;
            if(get.endsWith("}\r\n")||get.endsWith(0x16)){//TODO 待修改判断包尾
//                SyncNum--;
                getString = get;
//                qDebug()<<"thread"<<get.toHex();
                emit sig_readfinish(0);
                get.clear();
                break;
            }
        }
    }
}

QStringList serial_func::findAvailablePort()
{
    QStringList PortList;
    QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts();
    foreach(const QSerialPortInfo serialPortInfo, serialPortInfoList){
        PortList<<serialPortInfo.portName();
    }
    PortList.sort();
    return PortList;
}
